#version 300 es

uniform sampler2D _tex;
uniform vec4 _effect;
uniform vec4 _disturb;

in vec2 frag_uv;

out vec4 FragColor;

float clamp_x(float x) {
    float y = max(_effect.y, _effect.z);
    float z = min(_effect.y, _effect.z);

    z = min(z, 0.99f);
    y = max(y, 0.01f);

    float dist = max(y - z, 0.01f);
    y = z + dist;

    return (clamp(x, z, y) - z) / dist;
}

void main() {
    vec2 dir_center = (frag_uv * 2.0f) - 1.0f;
    float dist_s = max(0.0f, pow(max(0.0f, distance(dir_center, vec2(0.0f, 0.0f)) - _disturb.y), _disturb.z));
    vec2 dir = normalize(dir_center);

    vec2 uv = frag_uv - 0.2f * dir * dist_s * _disturb.x;

    vec4 color = texture(_tex, uv);
    color.x = clamp_x(pow(color.x, _effect.x));
    color.y = clamp_x(pow(color.y, _effect.x));
    color.z = clamp_x(pow(color.z, _effect.x));

    FragColor = vec4(color.xyz, 1);
}